package com.richstone.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.richstone.dao.SpkParaMapper;
import com.richstone.dto.PageType;
import com.richstone.dto.PageVo;
import com.richstone.entity.SpkPara;
import com.richstone.entity.SpkParaExample;
import com.richstone.entity.SpkParaExample.Criteria;
import com.richstone.entity.SpkParaKey;

@RestController
@RequestMapping("/SpkPara")
public class SpkParamController {

	@Autowired
	private SpkParaMapper spkParaMapper;

	@Autowired
	private PageHelper pgeHelper;

	@PostMapping("/query")
	public PageVo<SpkPara> query(PageType<SpkPara> page, SpkPara spkPara) {
		
		SpkParaExample spkParaExample = new SpkParaExample();
		
		Criteria criteria1 = spkParaExample.or();
		//Criteria criteria2 = spkParaExample.or();
		//Criteria criteria3 = spkParaExample.or();
		
		if (spkPara.getFuncName() != null && !spkPara.getFuncName().equals(""))
			criteria1.andFuncNameLike("%" + spkPara.getFuncName() + "%");
		if (spkPara.getParaName() != null && !spkPara.getParaName().equals(""))
			criteria1.andParaNameLike("%" + spkPara.getParaName() + "%");
		if (spkPara.getParaCode() != null && !spkPara.getParaCode().equals(""))
			criteria1.andParaCodeEqualTo(spkPara.getParaCode());

		Page<SpkPara> page1 = PageHelper.startPage(page.getPageindex(), page.getPagesize());
		spkParaMapper.selectByExample(spkParaExample);
		List<SpkPara> lists = page1.getResult();
		PageVo<SpkPara> pagevo = new PageVo<SpkPara>();
		
		Page<SpkPara> page2 = PageHelper.startPage(1, -1);
		spkParaMapper.selectByExample(spkParaExample);
		long count = page2.getTotal();

		pagevo.setDatas(lists);
		pagevo.setDataCount(lists.size());

		int start = page.getPagesize() * (page.getPageindex() - 1) + 1;
		pagevo.setStartLine(start);
		pagevo.setEndLine(start + lists.size() - 1);
		pagevo.setPageCount((int) (count / page.getPagesize()+1));
		pagevo.setRecordCount((int) count);

		return pagevo;

	}

	@PostMapping("/insert")
	public int  insert(SpkPara spkPara,SpkParaKey key){
		if(spkParaMapper.selectByPrimaryKey(key)!=null){
			return 0;
		}
		spkPara.setOpTime(new Date());
		int i=spkParaMapper.insert(spkPara);
		if(i>0){
			return 1;
		}else{
			return 2;
		}
	}
	@PostMapping("/del")
	public int del(SpkParaKey key){
		
		int i=spkParaMapper.deleteByPrimaryKey(key);
		
		return i;
		
	}
	
	@PostMapping("/getByKey")
	public SpkPara getByKey(SpkParaKey key){
		return spkParaMapper.selectByPrimaryKey(key);
	}
	@PostMapping("/modify")
	public int modify(SpkPara spkPara){
		int i=spkParaMapper.updateByPrimaryKeySelective(spkPara);
		return i;
	}
}
